﻿(function () {
    "use strict";
    var videoData = null, newsData = null, pictureData = null;
    WinJS.UI.Pages.define("/pages/home/home.html", {
        // 每当用户导航至此页面时都要调用此功能。它
        // 使用应用程序的数据填充页面元素。
        ready: function (element, options) {
            //$('#test').html(videoData.toString())
            var element = element;
            var _this = this;
            page.fun.confirmNetwork(_this, element, this._init);
            WinJS.Utilities.query('.internalLink').listen("click", _this.linkClickEventHandler, false);
        },

        unload: function () {
            // TODO: 响应导航到其他页。
        },

        updateLayout: function (element, viewState, lastViewState) {
            element.querySelector("#video-listView").winControl.forceLayout();
            element.querySelector("#news-listView").winControl.forceLayout();
            element.querySelector("#picture-listView").winControl.forceLayout();
        },

        _init: function (element) {
            var _this = this;
            if (!videoData) {
                videoData = page.fun.getVideoDataList('http://app.hbook.us/flashinterface/GetMovieByPage.ashx?page=1&pagesize=10&&topicid=5');
                newsData = page.fun.getNewsDataList('http://app.hbook.us/flashinterface/Getnewsbypage.ashx?page=1&pagesize=11&sort=2&topicid=5');
                pictureData = page.fun.getPictureDataList('http://app.hbook.us/flashinterface/GetPicturesByPage.ashx?page=1&pagesize=10&topicid=5');
            }
            page.fun.bindDate(
                {
                    dataType: "video",
                    element: element,
                    dataList: videoData,
                    listviewID: "#video-listView",
                    listTemplate: "#frontTemplateWrap",
                    LayoutType: "GridLayout"
                }
            );
            page.fun.bindDate(
                {
                    dataType: "news",
                    element: element,
                    dataList: newsData,
                    listviewID: "#news-listView",
                    listTemplate: "#frontTemplateWrap",
                    LayoutType: "GridLayout"
                }
            );
            page.fun.bindDate(
                {
                    dataType: "picture",
                    element: element,
                    dataList: pictureData,
                    listviewID: "#picture-listView",
                    listTemplate: "#frontTemplateWrap",
                    LayoutType: "GridLayout"
                }
            );
            if (page.frontItemCount < 3) {
                var interval = setInterval(function () {
                    if (page.frontItemCount == 3) {
                        //$('#test').html(page.frontItemCount);
                        clearInterval(interval);
                        setTimeout(function () {
                            $('#content-wrap').css("visibility", "visible");
                            element.querySelector("#video-listView").winControl.forceLayout();
                            element.querySelector("#news-listView").winControl.forceLayout();
                            element.querySelector("#picture-listView").winControl.forceLayout();
                            $('.homeLoading').hide()
                        }, 2000)

                    }
                }, 20)
            }
            else {
                //$('#test').html(page.frontItemCount);
                $('.homeLoading').hide();
                setTimeout(function () {
                    $('#content-wrap').css("visibility", "visible")
                },500)
                
            }
        },

        linkClickEventHandler: function (eventInfo) {
        eventInfo.preventDefault();
        var link = eventInfo.target;
        WinJS.Navigation.navigate(link.href);
       }
    });
})();
